<template>
	<HyCard title="专家抽取方式">
		<HyForm :hyFormDefault="hyFormDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef">
			<template #custom>
				<ExpertTable :routerParams="props.routerParams" ref="expertTableRef1" v-if="checkData.includes(0)" :title="'回避专家'" />
				<div class="mt-5 w-full">
					<ExpertTable :routerParams="props.routerParams" ref="expertTableRef2" v-if="checkData.includes(1)" :title="'指定专家'" />
				</div>
			</template>
		</HyForm>
	</HyCard>
</template>
<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import HyCard from '/@/components/HyCard/index.vue';
import { useDict } from '/@/hooks/dict';
import ExpertTable from '../ExpertTable/index.vue';
// 定义字典
const { ethnic_group, industry_fields, professional_fields } = useDict('ethnic_group', 'industry_fields', 'professional_fields');
const props = defineProps({
	routerParams: {
		type: Object,
		default: () => {
			return {};
		},
	},
	detailData: {
		type: Object,
		default: () => {
			return {};
		},
	},
	expertSpecifiedData: {
		type: Object,
		default: () => {
			return [];
		},
	},
	expertAvoidanceData: {
		type: Object,
		default: () => {
			return [];
		},
	},
});
//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		expertExtractionTypeList: {
			label: '抽取方式',
			type: 'checkBox',
			required: true,
			width: '100%',
			options: [
				{
					label: '随机抽取',
					value: 0,
				},
				{
					label: '指定抽取',
					value: 1,
				},
			],
			change: (value: any) => change(value),
		},
		expertManagerCount: {
			label: '管理类专家人数',
			type: 'inputNumber',
			required: true,
			remove: !checkData.value.includes(0),
		},
		expertTechnologyCount: {
			label: '技术类专家人数',
			type: 'inputNumber',
			required: true,
			remove: !checkData.value.includes(0),
		},
		expertIndustryFields: {
			label: '所需行业领域',
			type: 'select',
			multiple: true,
			options: industry_fields.value,
			remove: !checkData.value.includes(0),
		},
		expertProfessionalFields: {
			label: '所需专业领域',
			type: 'select',
			options: professional_fields.value,
			multiple: true,
			remove: !checkData.value.includes(0),
		},
		expertAvoidanceUnits: {
			label: '回避单位',
			type: 'select',
			allowCreate: true,
			defaultFirstOption: true,
			multiple: true,
			remove: !checkData.value.includes(0),
		},
		expertSpecifiedCause: {
			label: '指定专家理由',
			type: 'textarea',
			options: ethnic_group.value,
			required: true,
			remove: !checkData.value.includes(1),
		},
	};
});

const hyFormRef = ref();
//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '150px',
		type: props.routerParams.operateType === 'add' ? 'submit' : 'view',
	};
});

const checkData = ref<number[]>([]);
const change = (value: number[]) => {
	checkData.value = value;
};
//必填校验
const expertTableRef1 = ref();
const expertTableRef2 = ref();

const check = async () => {
	const result = await hyFormRef.value.check();
	return result;
};
const getData = () => {
	return {
		...hyFormRef.value.hyForm,
		expertAvoidanceList: expertTableRef1.value?.tableData || [],
		expertSpecifiedList: expertTableRef2.value?.tableData || [],
	};
};

const hyFormDefault = ref({});
watch(
	() => props.detailData,
	(nv1: any) => {
		hyFormDefault.value = nv1;
		change(nv1.expertExtractionTypeList);
	},
	{ immediate: true }
);
watch(
	() => props.expertAvoidanceData,
	(nv1: any) => {
		if (nv1 && nv1.length > 0) {
			nextTick(() => {
				expertTableRef1.value.tableData = nv1;
			});
		}
	},
	{ immediate: true }
);
watch(
	() => props.expertSpecifiedData,
	(nv1: any) => {
		if (nv1 && nv1.length > 0) {
			nextTick(() => {
				expertTableRef2.value.tableData = nv1;
			});
		}
	},
	{ immediate: true }
);
defineExpose({ getData, check });
</script>
<style scoped lang="scss"></style>
